package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class SearchApiController {


    private final SearchService searchService;

    @Autowired
    public SearchApiController(SearchService searchService) {
        this.searchService = searchService;
    }
    /**
     * @description: 上架专辑
     * @author Petrichor
     * @date 2023/12/26 18:48
     * @param albumId 专辑id
     * @return NULL
    **/
    @Operation(summary = "上架专辑")
    @GetMapping("/upperAlbum/{albumId}")
    public Result upperAlbum(@PathVariable(value = "albumId") Long albumId){
        searchService.upperAlbum(albumId);
        return Result.ok();
    }
    /**
     * @description: 下架专辑
     * @author Petrichor
     * @date 2023/12/27 19:00
     * @param albumId 专辑id
     * @return NULL
    **/
    @Operation(summary = "下架专辑")
    @GetMapping("/lowerAlbum/{albumId}")
    public Result lowerAlbum(@PathVariable Long albumId) {
        searchService.lowerAlbum(albumId);
        return Result.ok();
    }
    /**
     * @description: 批量上架专辑
     * @author Petrichor
     * @date 2023/12/27 16:41
     * @return
    **/
    @Operation(summary = "批量上架")
    @GetMapping("/batchUpperAblum")
    public Result upperAlbum(){
        for (long i = 2; i <= 1577; i++) {
            searchService.upperAlbum(i);
        }
        return Result.ok();
    }
    /**
     * @description: 检索功能实现 当点击搜索按钮
     * @author Petrichor
     * @date 2023/12/27 18:43
     * @param albumIndexQuery 专辑信息搜索
     * @return albumSearchResponseVo 检索出来的商品信息
    **/
    @Operation(summary = "专辑列表搜素")
    @PostMapping
    public Result search(@RequestBody AlbumIndexQuery albumIndexQuery) throws IOException {
        AlbumSearchResponseVo albumSearchResponseVo = searchService.search(albumIndexQuery);
        return Result.ok(albumSearchResponseVo);
    }
    /**
     * @description: 关键字自动补全
     * @author Petrichor
     * @date 2023/12/28 21:04
     * @param keyword 关键字
     * @return
    **/
    @Operation(summary = "关键字自动补全")
    @GetMapping("completeSuggest/{keyword}")
    public Result completeSuggest(@PathVariable String keyword) {
        List<String> list = searchService.completeSuggest(keyword);
        return Result.ok(list);
    }
    /**
     * @description: 更新排行榜数据
     * @author Petrichor
     * @date 2024/1/2 16:46
    **/
    @SneakyThrows  // 将当前方法抛出的异常，包装成RuntimeException，骗过编译器，使得调用点可以不用显示处理异常信息。
    @Operation(summary = "更新排行榜")
    @GetMapping("/updateLatelyAlbumRanking")
    public Result updateLatelyAlbumRanking() {
        searchService.updateLatelyAlbumRanking();
        return Result.ok();
    }
    /**
     * @description: 获取排行榜列表
     * @author Petrichor
     * @date 2024/1/2 19:16
     * @param category1Id 一级分类id
     * @param dimension 热度:hotScore、播放量:playStatNum、订阅量:subscribeStatNum、购买量:buyStatNum、评论数:albumCommentStatNum
     * @return List<AlbumInfoIndexVo> 返回专辑列表
    **/
    @Operation(summary = "获取排行榜列表")
    @Parameters({
            @Parameter(name = "category1Id",description = "一级分类",in = ParameterIn.PATH,required = true),
            @Parameter(name = "dimension",description = "热度:hotScore、播放量:playStatNum、订阅量:subscribeStatNum、购买量:buyStatNum、评论数:albumCommentStatNum",required = true,in = ParameterIn.PATH),
    })
    @GetMapping("findRankingList/{category1Id}/{dimension}")
    public Result<List<AlbumInfoIndexVo>> findRankingList(@PathVariable Long category1Id, @PathVariable String dimension) {
        List<AlbumInfoIndexVo> infoIndexVoList = searchService.findRankingList(category1Id, dimension);
        return Result.ok(infoIndexVoList);
    }
    /**
     * @description: 用于[service.service-album]/com/atguigu/tingshu/album/canal/CanalClientListener.java
     *               的远程调用,更新统计数据
     * @author Petrichor
     * @date 2024/1/5 19:26
     * @param albumId 专辑id
     * @param statType 统计类型：0401-播放量 0402-订阅量 0403-购买量 0403-评论数
     * @param statNum 统计数目
     * @return NULL
    **/
    @PutMapping(value = "/updateAlbumStat/{albumId}/{statType}/{statNum}")
    public Result updateAlbumStat(@PathVariable(value = "albumId") Long albumId ,
                                  @PathVariable(value = "statType") String statType ,
                                  @PathVariable(value = "statNum")Integer statNum) {
        searchService.updateAlbumStat(albumId , statType , statNum) ;
        return Result.ok() ;
    }
}

